﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;

namespace WISD.Common
{
    public static class Extend
    {
        /// <summary>
        /// 获得某指定属性的值
        /// </summary>
        /// <param name="o"></param>
        /// <param name="propertyName">属性名</param>
        /// <returns></returns>
        //public static T GetPropertyValue<T>(this object o, string propertyName)
        //{
        //    Type type = o.GetType();
        //    ParameterExpression parameter = Expression.Parameter(type, "p");
        //    MemberInfo[] properties = type.GetMember(propertyName);
        //    if (properties.Count() > 0)
        //    {
        //        MemberInfo property = properties.First();
        //        MemberExpression body = Expression.MakeMemberAccess(parameter, property);
        //        LambdaExpression lambda = Expression.Lambda(body, parameter);
        //        Delegate func = lambda.Compile();
        //        object value = func.DynamicInvoke(o);
        //        if (value.GetType() == type)
        //        {
        //            return (T)value;
        //        }
        //        else
        //        {
        //            try
        //            {
        //                return (T)Convert.ChangeType(value, type);
        //            }
        //            catch (Exception ex)
        //            {
        //                throw ex;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        string errMsg = string.Format("对象{0}不存在名为{1}的属性", o.ToString(), propertyName);
        //        throw new Exception(errMsg);
        //    }
        //}

      

        public static bool ToBool(this int? value)
        {
            if (value != 1)
            {
                return false;
            }
            return true;
        }

        public static int ToInt(this bool value)
        {
            if (value)
            {
                return 1;
            }
            return 0;
        }


        public static long ToLong(this object o)
        {
            if (o == null)
            {
                return -1;
            }
            else
            {
                long result;
                if (long.TryParse(o.ToString(), out result))
                {
                    return result;
                }
                return -1;
            }
        }

        public static int ToInt(this object o)
        {
            if (o == null)
            {
                return -1;
            }
            else
            {
                int result;
                if (int.TryParse(o.ToString(), out result))
                {
                    return result;
                }
                return -1;
            }
        }

        public static long ToLong(this string value)
        {
            long result = 0;
            long.TryParse(value, out result);
            return result;
        }

        public static long? ToLongNullable(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return null;
            }
            else
            {
                long result;
                if (long.TryParse(value, out result))
                {
                    return result;
                }
                return null;
            }
        }

        public static int? ToIntNullable(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return null;
            }
            else
            {
                int result;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
                return null;
            }
        }

        public static int ToInt(this string value)
        {
            int result = 0;
            int.TryParse(value, out result);
            return result;
        }

        public static bool? ToBoolNullable(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return null;
            }
            else
            {
                bool result;
                if (bool.TryParse(value, out result))
                {
                    return result;
                }
                return null;
            }
        }

        public static bool ToBool(this string value)
        {
            bool result = false;
            bool.TryParse(value, out result);
            return result;
        }

        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }
    }
}
